home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 1996 April / Software of the Month Club 1996 April.iso / pc / os2 / psutils / src / pserror.c < prev    next >
C/C++ Source or Header  |  1996-02-21  |  3KB  |  123 lines

  1. /* pserror.c
  2.  * Copyright (C) Angus J. C. Duggan 1991-1995
  3.  * See file LICENSE for details.
  4.  *
  5.  * Warnings and errors for PS programs
  6.  */
  7.  
  8. extern char *program ;    /* Defined by main program, giving program name */
  9.  
  10. #include "psutil.h"
  11. #include "pserror.h"
  12. #include "patchlev.h"
  13.  
  14. #include <string.h>
  15.  
  16. /* Message functions; there is a single are varargs functions for messages,
  17.    warnings, and errors sent to stderr. If called with the flags MESSAGE_EXIT
  18.    set, the routine does not return */
  19.  
  20. #define MAX_MESSAGE    256    /* maximum formatted message length */
  21. #define MAX_FORMAT    16    /* maximum format length */
  22. #define MAX_COLUMN    78    /* maximum column to print upto */
  23.  
  24. void message(int flags, char *format, ...)
  25. {
  26.   va_list args ;
  27.   static column = 0 ;        /* current screen column for message wrap */
  28.   char msgbuf[MAX_MESSAGE] ;    /* buffer in which to put the message */
  29.   char *bufptr = msgbuf ;    /* message buffer pointer */
  30.  
  31.   if ( (flags & MESSAGE_NL) && column != 0 ) {    /* new line if not already */
  32.     putc('\n', stderr) ;
  33.     column = 0 ;
  34.   }
  35.     
  36.   if ( flags & MESSAGE_PROGRAM ) {
  37.     strcpy(bufptr, program) ;
  38.     bufptr += strlen(program) ;
  39.     *bufptr++ = ':' ;
  40.     *bufptr++ = ' ' ;
  41.   }
  42.  
  43.   va_start(args, format) ;
  44.   if ( format != NULL ) {
  45.     char c ;
  46.     while ( (c = *format++) != '\0' ) {
  47.       if (c == '%') {
  48.     int done, longform, index ;
  49.     char fmtbuf[MAX_FORMAT] ;
  50.     longform = index = 0 ;
  51.     fmtbuf[index++] = c ;
  52.     do {
  53.       done = 1 ;
  54.       fmtbuf[index++] = c = *format++ ;
  55.       fmtbuf[index] = '\0' ;
  56.       switch (c) {
  57.       case '%':
  58.         *bufptr++ = '%' ;
  59.       case '\0':
  60.         break ;
  61.       case 'e': case 'E': case 'f': case 'g': case 'G':
  62.         {
  63.           double d = va_arg(args, double) ;
  64.           sprintf(bufptr, fmtbuf, d) ;
  65.           bufptr += strlen(bufptr) ;
  66.         }
  67.         break ;
  68.       case 'c': case 'd': case 'i': case 'o':
  69.       case 'p': case 'u': case 'x': case 'X':
  70.         if ( longform ) {
  71.           long l = va_arg(args, long) ;
  72.           sprintf(bufptr, fmtbuf, l) ;
  73.         } else {
  74.           int i = va_arg(args, int) ;
  75.           sprintf(bufptr, fmtbuf, i) ;
  76.         }
  77.         bufptr += strlen(bufptr) ;
  78.         break ;
  79.       case 's':
  80.         {
  81.           char *s = va_arg(args, char *) ;
  82.           sprintf(bufptr, fmtbuf, s) ;
  83.           bufptr += strlen(bufptr) ;
  84.         }
  85.         break ;
  86.       case 'l':
  87.         longform = 1 ;
  88.         /* FALLTHRU */
  89.       default:
  90.         done = 0 ;
  91.       }
  92.     } while ( !done ) ;
  93.       } else if ( c == '\n' ) {    /* write out message so far and reset column */
  94.     int len = bufptr - msgbuf ;    /* length of current message */
  95.     *bufptr++ = '\n' ;
  96.     *bufptr = '\0' ;
  97.     if ( column + len > MAX_COLUMN && column > 0 ) {
  98.       putc('\n', stderr) ;
  99.       column = 0 ;
  100.     }
  101.     fputs(bufptr = msgbuf, stderr) ;
  102.     column = 0 ;
  103.       } else
  104.     *bufptr++ = c ;
  105.     }
  106.     *bufptr = '\0' ;
  107.     {
  108.       int len = bufptr - msgbuf ;    /* length of current message */
  109.       if ( column + len > MAX_COLUMN && column > 0 ) {
  110.     putc('\n', stderr) ;
  111.     column = 0 ;
  112.       }
  113.       fputs(msgbuf, stderr) ;
  114.       column += len ;
  115.     }
  116.     fflush(stderr) ;
  117.   }
  118.   va_end(args) ;
  119.  
  120.   if ( flags & MESSAGE_EXIT )    /* don't return to program */
  121.     exit(1) ;
  122. }
  123.